import React from 'react'
import { Modal,Form,Input,Select } from 'antd'
const FormItem = Form.Item
const Option = Select.Option
class Modals extends React.Component{
	constructor(props){
		super(props)
		this.state = {
			
		}
	}
	handleChange(value){
//		console.log(value)
	}
	onOk=()=> {
	  const {validateFields,getFieldsValue,getFieldError}=this.props.form
      const {onUpdate,info,isNew,onNew} = this.props

      validateFields((errors) => {
        if (errors) {
          return
        }
        const params=getFieldsValue()
//		var childId = ''
//		params.child.map((item,i)=>{
//			childId += item + ','
//		})
//		var data = {}
//		data.child = child
//		data.id = info.id
		delete params.child
//		params.childId = childId
		if(isNew != true){
			let data = params
			data.id = info.id
//			console.log(data)
			onUpdate(data)
		}else{
//			console.log(params)
			onNew(params)
		}
      })
	}
	render(){
		const {getFieldDecorator}=this.props.form
		const formItemLayout = {
          labelCol: {
            span: 6
          },
          wrapperCol: {
            span: 14
          }
        }
		const { visible,onCancel,info,isNew,orginList } = this.props
		if(!orginList){
			return null
		}
		const selectList = []
		selectList.push(<Option key="0" value={0}>无上级</Option>)
		orginList.map((item,i) =>{
			selectList.push(<Option key={item.id} value={item.id}>{item.name}</Option>)
		})
		const infoValue = []
		if(isNew == false && info.children){
			info.children.map((item,j)=>{
				infoValue.push(item.id.toString())
			})
		}
		
		const modalProps = {
			  title:'编辑机构',
	          visible:visible,
	          onOk: this.onOk,
	          onCancel: onCancel,
	          wrapClassName: 'vertical-center-modal'
		}
		
		return(
			<Modal {...modalProps}>
				<Form>
					<FormItem label='机构编号' hasFeedback {...formItemLayout}>
						{getFieldDecorator('number', {
		  			      initialValue:isNew?'':info?info.number:'',
		                  rules: [
		                    {
		                      required: false,
		                      message: '未填写'
		                    }
		                  ]
		                })(
		                  <Input disabled={isNew?false:true} />
		                )}
					</FormItem>	
					<FormItem label='机构名称' hasFeedback {...formItemLayout}>
						{getFieldDecorator('name', {
		  			      initialValue:isNew?'':info?info.name:'',
		                  rules: [
		                    {
		                      required: false,
		                      message: '未填写'
		                    }
		                  ]
		                })(
		                  <Input disabled={isNew?false:true} />
		                )}
					</FormItem>
					<FormItem label='联系方式' hasFeedback {...formItemLayout}>
						{getFieldDecorator('contactWay', {
		  			      initialValue:isNew?'':info?info.contactWay:'',
		                  rules: [
		                    {
		                      required: false,
		                      message: '未填写'
		                    }
		                  ]
		                })(
		                  <Input disabled={isNew?false:true} />
		                )}
					</FormItem>
					<FormItem label='机构地址' hasFeedback {...formItemLayout}>
						{getFieldDecorator('address', {
		  			      initialValue:isNew?'':info?info.address:'',
		                  rules: [
		                    {
		                      required: false,
		                      message: '未填写'
		                    }
		                  ]
		                })(
		                  <Input disabled={isNew?false:true} />
		                )}
					</FormItem>
					<FormItem label='上级机构' hasFeedback {...formItemLayout}>
						{getFieldDecorator('parentId', {
		  			      initialValue:isNew?"":info?info.parentId:"",
		                  rules: [
		                    {
		                      required: false,
		                      message: '未填写'
		                    }
		                  ]
		                })(
		                  <Select onChange={this.handleChange}
		                  >	
		                  	{selectList}
						   
						  </Select>
		                )}
					</FormItem>
					{/*<FormItem label='下级机构' hasFeedback {...formItemLayout}>
						{getFieldDecorator('child', {
		  			      initialValue:isNew?[]:info?infoValue:[],
		                  rules: [
		                    {
		                      required: false,
		                      message: '未填写'
		                    }
		                  ]
		                })(
		                  <Select onChange={this.handleChange}
		                  	mode="multiple"	
		                  >	
		                  	{selectList}
						   
						  </Select>
		                )}
					</FormItem>*/}
				</Form>
			</Modal>
		)
	}
}

export default Form.create()(Modals)
